package com.itheima.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mm.mapper.CompanyMapper;
import com.itheima.mm.pojo.Company;
import com.itheima.mm.pojo.PageResult;
import com.itheima.mm.service.CompanyService;
import com.itheima.mm.dto.CompanDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * heimamm
 *
 * @ClassName CompanyServiceImpl
 * @Description
 * @Author 吴志强 --游侠波克比
 * @Date 2022/1/10 15:16
 * @Version V1.0
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;

    @Override
    public PageResult pageAll(CompanDto companyVO) {
        //全查询,分页
        //分页过滤
        Integer page = companyVO.getPage();
        Integer pageSize = companyVO.getPagesize();
        Page<Company> companyPage = new Page<>(page, pageSize);

        //条件过滤
        String city = companyVO.getCity();
        String province = companyVO.getProvince();
        String shortName = companyVO.getShortName();
        String tags = companyVO.getTags();
        Boolean state = companyVO.getState();
        LambdaQueryWrapper<Company> companyLqw = new LambdaQueryWrapper<>();
        companyLqw.like(city != null,Company::getCity,city);
        companyLqw.like(province != null,Company::getProvince,province);
        companyLqw.like(shortName != null,Company::getShortName,shortName);
        companyLqw.like(tags != null,Company::getTags,tags);
        companyLqw.like(state != null,Company::getState,state);

        Page<Company> selectPage = companyMapper.selectPage(companyPage, companyLqw);

        //封装返回类
        return new PageResult(selectPage.getTotal(),selectPage.getSize(),selectPage.getPages(),selectPage.getCurrent(),selectPage.getRecords());
    }

    @Override
    public Boolean updateState(Integer id, Integer state) {
        //用户更具id修改状态
        LambdaUpdateWrapper<Company> companyLuw = new LambdaUpdateWrapper<>();
        companyLuw.eq(Company::getId,id);
        companyLuw.set(Company::getState,state);
        companyMapper.update(null,companyLuw);
        return true;
    }
}
